package com.acme.bankapp.service.bank;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.acme.bankapp.domain.bank.EMail;

/**
 * Class {@code EMailService} implements sending e-mails routine.
 * @author Artem Nikitin
 *
 */
public class EMailService {
	
	private boolean isStopped;
	private ExecutorService mailThreadPool;
	
	static class EMailHandler implements Runnable {

		private EMail mail;
		
		public EMailHandler(EMail mail) {
			this.mail = mail;
		}

		@Override
		public void run() {
			System.out.println("Sending mail... " + mail);
		}
		
	}
	
	public EMailService() {
		mailThreadPool = Executors.newFixedThreadPool(10);
	}
	

	public void sendMail(EMail eMail) {
		if (isStopped) {
			throw new IllegalStateException("Service is stopped");
		}
		mailThreadPool.execute(new EMailHandler(eMail));
	}
	
	/**
	 * Closes service. Service is no longer available after this method call 
	 */
	public void close() {
		isStopped = true;
		mailThreadPool.shutdown();
	}
}
